/**
 * 
 * 起迪科技 Copyright (c) 2014-2018 QiDi,Inc.All Rights Reserved.
 */
package cn.qidisoft.edu.hzjt.service.impl;

import java.util.List;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.qidisoft.core.common.code.QdfRespCode;
import cn.qidisoft.core.common.exception.dao.DBException;
import cn.qidisoft.core.common.log.LoggerAdapter;
import cn.qidisoft.core.common.log.LoggerAdapterFactory;
import cn.qidisoft.core.common.pagination.Page;
import cn.qidisoft.core.common.pagination.PaginationBean;
import cn.qidisoft.edu.hzjt.dao.KtglReviewExpertMapper;
import cn.qidisoft.edu.hzjt.dao.KtglReviewGroupMapper;
import cn.qidisoft.edu.hzjt.dao.KtglTeacherThesisReviewMapper;
import cn.qidisoft.edu.hzjt.model.Employee;
import cn.qidisoft.edu.hzjt.model.KtglReviewExpert;
import cn.qidisoft.edu.hzjt.model.KtglReviewGroup;
import cn.qidisoft.edu.hzjt.model.KtglTeacherThesisReview;
import cn.qidisoft.edu.hzjt.service.IKtglReviewGroupService;
import cn.qidisoft.edu.hzjt.utils.LogType;
import cn.qidisoft.edu.hzjt.vo.TeacherThesis;

/**
 * 
 * @author hxq
 * @version $Id: KtglReviewGroupServiceImpl.java, v 0.1 2018年12月11日 上午10:04:13 hxq Exp $
 */
@Service("ktglReviewGroupService")
public class KtglReviewGroupServiceImpl implements IKtglReviewGroupService {
  protected static final LoggerAdapter LOGGER =
      LoggerAdapterFactory.getLogger(LogType.HZJT_LOGGER.val);
  @Autowired
  private KtglReviewExpertMapper ktglReviewExpertMapper;
  @Autowired
  private KtglReviewGroupMapper ktglReviewGroupMapper;
  @Autowired
  private KtglTeacherThesisReviewMapper ktglTeacherThesisReviewMapper;

  /**
   * @see cn.qidisoft.edu.hzjt.service.IKtglReviewGroupService#deleteReviewGroupByPK(java.lang.Integer)
   */
  @Override
  public int deleteReviewGroupByPK(Integer groupid) {
    try {
      return ktglReviewGroupMapper.deleteByPK(groupid);
    } catch (Exception e) {
      e.printStackTrace();
      LOGGER.error("deleteReviewGroupByPK [{}] error," + this.getClass().getName() + "=[{}]",
          this.getClass().getName(),
          ToStringBuilder.reflectionToString(groupid, ToStringStyle.SHORT_PREFIX_STYLE));
      throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(), e);
    }
  }

  /**
   * @see cn.qidisoft.edu.hzjt.service.IKtglReviewGroupService#addReviewGroup(cn.qidisoft.edu.hzjt.model.KtglReviewGroup)
   */
  @Override
  @Transactional
  public int addReviewGroup(KtglReviewGroup record) {
    try {
      int num = ktglReviewGroupMapper.insert(record);
      if (num >= 1) {
        String[] empids = record.getEmpids();
        if (empids != null && empids.length >= 1) {
          KtglReviewExpert ktglReviewExpert = null;
          for (String empid : empids) {
            num = 0;
            ktglReviewExpert = new KtglReviewExpert();
            ktglReviewExpert.setEmpid(Integer.parseInt(empid));
            ktglReviewExpert.setGroupid(record.getGroupid());
            ktglReviewExpert.setStatus(true);
            num = ktglReviewExpertMapper.insert(ktglReviewExpert);
          }
        }
      }
      return num >= 1 ? record.getGroupid() : 0;
    } catch (Exception e) {
      e.printStackTrace();
      LOGGER.error("addReviewGroup [{}] error," + this.getClass().getName() + "=[{}]",
          this.getClass().getName(),
          ToStringBuilder.reflectionToString(record, ToStringStyle.SHORT_PREFIX_STYLE));
      throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(), e);
    }
  }

  /**
   * @see cn.qidisoft.edu.hzjt.service.IKtglReviewGroupService#queryReviewGroupByPK(java.lang.Integer)
   */
  @Override
  public KtglReviewGroup queryReviewGroupByPK(Integer groupid) {
    try {
      return ktglReviewGroupMapper.queryByPK(groupid);
    } catch (Exception e) {
      e.printStackTrace();
      LOGGER.error("queryReviewGroupByPK [{}] error," + this.getClass().getName() + "=[{}]",
          this.getClass().getName(),
          ToStringBuilder.reflectionToString(groupid, ToStringStyle.SHORT_PREFIX_STYLE));
      throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(), e);
    }
  }

  /**
   * @see cn.qidisoft.edu.hzjt.service.IKtglReviewGroupService#updateReviewGroupByPK(cn.qidisoft.edu.hzjt.model.KtglReviewGroup)
   */
  @Override
  @Transactional
  public int updateReviewGroupByPK(KtglReviewGroup record) {
    try {
      // 修改主表信息
      int num = ktglReviewGroupMapper.updateByPK(record);
      if (num >= 1) {
        // 1.先批量将成员表的状态修改成不正常
        KtglReviewExpert ktglReviewExpert = new KtglReviewExpert();
        ktglReviewExpert.setGroupid(record.getGroupid());
        ktglReviewExpert.setStatus(false);
        ktglReviewExpert.setStep(1);
        num = ktglReviewExpertMapper.updateReviewExpertStatus(ktglReviewExpert);
        // 2.然后将正常的成员信息状态修改成正常，记录修改失败的成员数据
        if (num >= 1) {
          if (record.getEmpids() != null && record.getEmpids().length >= 1) {
            for (String empid : record.getEmpids()) {
              ktglReviewExpert = new KtglReviewExpert();
              ktglReviewExpert.setEmpid(Integer.parseInt(empid));
              ktglReviewExpert.setStatus(true);
              ktglReviewExpert.setGroupid(record.getGroupid());
              ktglReviewExpert.setStep(2);
              num = ktglReviewExpertMapper.updateReviewExpertStatus(ktglReviewExpert);
              if (num == 0) {
                // 3.将修改不成功的成员数据新增到成员表中
                num = ktglReviewExpertMapper.insert(ktglReviewExpert);
              }
            }
          }
        } else { // 标识状态全部修改失败
          // 3.将修改不成功的成员数据新增到成员表中
          if (record.getEmpids() != null && record.getEmpids().length >= 1) {
            for (String empid : record.getEmpids()) {
              ktglReviewExpert = new KtglReviewExpert();
              ktglReviewExpert.setEmpid(Integer.parseInt(empid));
              ktglReviewExpert.setStatus(true);
              ktglReviewExpert.setGroupid(record.getGroupid());
              num = ktglReviewExpertMapper.insert(ktglReviewExpert);
            }
          }
        }
      }
      return num;
    } catch (Exception e) {
      e.printStackTrace();
      LOGGER.error("updateReviewGroupByPK [{}] error," + this.getClass().getName() + "=[{}]",
          this.getClass().getName(),
          ToStringBuilder.reflectionToString(record, ToStringStyle.SHORT_PREFIX_STYLE));
      throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(), e);
    }
  }

  /**
   * @see cn.qidisoft.edu.hzjt.service.IKtglReviewGroupService#queryKtglReviewGroupPageList(cn.qidisoft.edu.hzjt.model.KtglReviewGroup,
   *      cn.qidisoft.core.common.pagination.Page)
   */
  @Override
  public PaginationBean<KtglReviewGroup> queryKtglReviewGroupPageList(
      KtglReviewGroup ktglReviewGroup, Page page) {
    try {
      int c = ktglReviewGroupMapper.queryKtglReviewGroupPageCount(ktglReviewGroup);
      int totalRecords = 0;
      totalRecords = (int) c;
      PaginationBean<KtglReviewGroup> pageHolder =
          new PaginationBean<KtglReviewGroup>(page, totalRecords);
      List<KtglReviewGroup> resultList =
          ktglReviewGroupMapper.queryKtglReviewGroupPageList(ktglReviewGroup,
              new RowBounds((pageHolder.getCurrentPage() - 1) * pageHolder.getPageSize(),
                  pageHolder.getPageSize()));
      pageHolder.setPageList(resultList);
      return pageHolder;
    } catch (Exception e) {
      e.printStackTrace();
      LOGGER.error("queryKtglReviewGroupPageList [{}] error," + this.getClass().getName() + "=[{}]",
          this.getClass().getName(), ToStringBuilder.reflectionToString(
              ktglReviewGroup + "===" + page, ToStringStyle.SHORT_PREFIX_STYLE));
      throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(), e);
    }
  }

  /**
   * @see cn.qidisoft.edu.hzjt.service.IKtglReviewGroupService#queryVerifyReviewTeacherGroup(cn.qidisoft.edu.hzjt.model.KtglReviewGroup)
   */
  @Override
  public int queryVerifyReviewTeacherGroup(KtglReviewGroup ktglReviewGroup) {
    try {
      return ktglReviewGroupMapper.queryVerifyReviewTeacherGroup(ktglReviewGroup);
    } catch (Exception e) {
      e.printStackTrace();
      LOGGER.error(
          "queryVerifyReviewTeacherGroup [{}] error," + this.getClass().getName() + "=[{}]",
          this.getClass().getName(),
          ToStringBuilder.reflectionToString(ktglReviewGroup, ToStringStyle.SHORT_PREFIX_STYLE));
      throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(), e);
    }
  }

  /**
   * @see cn.qidisoft.edu.hzjt.service.IKtglReviewGroupService#queryReviewExpertList(java.lang.Integer)
   */
  @Override
  public List<KtglReviewExpert> queryReviewExpertList(Integer groupid) {
    try {
      KtglReviewExpert ktglReviewExpert = new KtglReviewExpert();
      ktglReviewExpert.setGroupid(groupid);
      return ktglReviewExpertMapper.queryReviewExpertList(ktglReviewExpert);
    } catch (Exception e) {
      e.printStackTrace();
      LOGGER.error("queryReviewExpertList [{}] error," + this.getClass().getName() + "=[{}]",
          this.getClass().getName(),
          ToStringBuilder.reflectionToString(groupid, ToStringStyle.SHORT_PREFIX_STYLE));
      throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(), e);
    }
  }

  /**
   * @see cn.qidisoft.edu.hzjt.service.IKtglReviewGroupService#queryReviewExpertByAllEmployee(java.lang.Integer)
   */
  @Override
  public List<Employee> queryReviewExpertByAllEmployee(Integer groupid) {
    try {
      KtglReviewExpert ktglReviewExpert = new KtglReviewExpert();
      ktglReviewExpert.setGroupid(groupid);
      return ktglReviewExpertMapper.queryReviewExpertByAllEmployee(ktglReviewExpert);
    } catch (Exception e) {
      e.printStackTrace();
      LOGGER.error(
          "queryReviewExpertByAllEmployee [{}] error," + this.getClass().getName() + "=[{}]",
          this.getClass().getName(),
          ToStringBuilder.reflectionToString(groupid, ToStringStyle.SHORT_PREFIX_STYLE));
      throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(), e);
    }
  }

  /**
   * @see cn.qidisoft.edu.hzjt.service.IKtglReviewGroupService#queryRealtionTeacherThesisCount(java.lang.String)
   */
  @Override
  public int queryRealtionTeacherThesisCount(String[] idList) {
    try {
      KtglReviewGroup ktglReviewGroup = new KtglReviewGroup();
      ktglReviewGroup.setGroupids(idList);
      return ktglReviewGroupMapper.queryRealtionTeacherThesisCount(ktglReviewGroup);
    } catch (Exception e) {
      e.printStackTrace();
      LOGGER.error(
          "queryRealtionTeacherThesisCount [{}] error," + this.getClass().getName() + "=[{}]",
          this.getClass().getName(),
          ToStringBuilder.reflectionToString(idList, ToStringStyle.SHORT_PREFIX_STYLE));
      throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(), e);
    }
  }

  /**
   * @see cn.qidisoft.edu.hzjt.service.IKtglReviewGroupService#deleteAllReviewGroupByPK(java.lang.String[])
   */
  @Override
  @Transactional
  public int deleteAllReviewGroupByPK(String[] idList) {
    try {
      // 先删除子表记录
      KtglReviewGroup ktglReviewGroup = new KtglReviewGroup();
      ktglReviewGroup.setGroupids(idList);
      ktglReviewExpertMapper.deleteAllByPK(ktglReviewGroup);
      // 在删除主表记录信息
      return ktglReviewGroupMapper.deleteAllByPK(ktglReviewGroup);
    } catch (Exception e) {
      e.printStackTrace();
      LOGGER.error("deleteAllReviewGroupByPK [{}] error," + this.getClass().getName() + "=[{}]",
          this.getClass().getName(),
          ToStringBuilder.reflectionToString(idList, ToStringStyle.SHORT_PREFIX_STYLE));
      throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(), e);
    }
  }

  /**
   * @see cn.qidisoft.edu.hzjt.service.IKtglReviewGroupService#queryTeacherThesisReviewPageList(cn.qidisoft.edu.hzjt.model.zyc.TeacherThesis,
   *      cn.qidisoft.core.common.pagination.Page)
   */
  @Override
  public PaginationBean<cn.qidisoft.edu.hzjt.vo.TeacherThesis> queryTeacherThesisReviewPageList(
      cn.qidisoft.edu.hzjt.vo.TeacherThesis data, Page page) {
    try {
      int c = ktglTeacherThesisReviewMapper.queryTeacherThesisReviewPageCount(data);
      int totalRecords = 0;
      totalRecords = (int) c;
      PaginationBean<cn.qidisoft.edu.hzjt.vo.TeacherThesis> pageHolder =
          new PaginationBean<cn.qidisoft.edu.hzjt.vo.TeacherThesis>(page, totalRecords);
      List<cn.qidisoft.edu.hzjt.vo.TeacherThesis> resultList =
          ktglTeacherThesisReviewMapper.queryTeacherThesisReviewPageList(data,
              new RowBounds((pageHolder.getCurrentPage() - 1) * pageHolder.getPageSize(),
                  pageHolder.getPageSize()));
      pageHolder.setPageList(resultList);
      return pageHolder;
    } catch (Exception e) {
      e.printStackTrace();
      LOGGER.error(
          "queryTeacherThesisReviewPageList [{}] error," + this.getClass().getName() + "=[{}]",
          this.getClass().getName(), ToStringBuilder.reflectionToString(data + "===" + page,
              ToStringStyle.SHORT_PREFIX_STYLE));
      throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(), e);
    }
  }

  /**
   * @see cn.qidisoft.edu.hzjt.service.IKtglReviewGroupService#queryTeacherThesisReviewList(cn.qidisoft.edu.hzjt.model.KtglTeacherThesisReview)
   */
  @Override
  public KtglTeacherThesisReview queryTeacherThesisReview(
      KtglTeacherThesisReview ktglTeacherThesisReview) {
    try {
      return ktglTeacherThesisReviewMapper.queryTeacherThesisReview(ktglTeacherThesisReview);
    } catch (Exception e) {
      e.printStackTrace();
      LOGGER.error("queryTeacherThesisReviewList [{}] error," + this.getClass().getName() + "=[{}]",
          this.getClass().getName(), ToStringBuilder.reflectionToString(ktglTeacherThesisReview,
              ToStringStyle.SHORT_PREFIX_STYLE));
      throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(), e);
    }
  }

  /**
   * @see cn.qidisoft.edu.hzjt.service.IKtglReviewGroupService#updateTeacherThesisReview(cn.qidisoft.edu.hzjt.model.KtglTeacherThesisReview)
   */
  @Override
  @Transactional
  public int updateTeacherThesisReview(KtglTeacherThesisReview ktglTeacherThesisReview) {
    try {
      // 首先判断是否修改还是新增
      Integer psid =
          ktglTeacherThesisReviewMapper.queryTeacherThesisReviewById(ktglTeacherThesisReview);
      if (psid == null) { // 表示不存在，新增
        return ktglTeacherThesisReviewMapper.insert(ktglTeacherThesisReview);
      } else { // 修改
        ktglTeacherThesisReview.setPsid(psid);
        return ktglTeacherThesisReviewMapper.updateByPK(ktglTeacherThesisReview);
      }
    } catch (Exception e) {
      e.printStackTrace();
      LOGGER.error("updateTeacherThesisReview [{}] error," + this.getClass().getName() + "=[{}]",
          this.getClass().getName(), ToStringBuilder.reflectionToString(ktglTeacherThesisReview,
              ToStringStyle.SHORT_PREFIX_STYLE));
      throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(), e);
    }
  }

  /**
   * @see cn.qidisoft.edu.hzjt.service.IKtglReviewGroupService#queryTeacherManagerThesisReviewPageList(cn.qidisoft.edu.hzjt.vo.TeacherThesis,
   *      cn.qidisoft.core.common.pagination.Page)
   */
  @Override
  public PaginationBean<TeacherThesis> queryTeacherManagerThesisReviewPageList(TeacherThesis data,
      Page page) {
    try {
      int c = ktglTeacherThesisReviewMapper.queryTeacherManagerThesisReviewPageCount(data);
      int totalRecords = 0;
      totalRecords = (int) c;
      PaginationBean<cn.qidisoft.edu.hzjt.vo.TeacherThesis> pageHolder =
          new PaginationBean<cn.qidisoft.edu.hzjt.vo.TeacherThesis>(page, totalRecords);
      List<cn.qidisoft.edu.hzjt.vo.TeacherThesis> resultList =
          ktglTeacherThesisReviewMapper.queryTeacherManagerThesisReviewPageList(data,
              new RowBounds((pageHolder.getCurrentPage() - 1) * pageHolder.getPageSize(),
                  pageHolder.getPageSize()));
      pageHolder.setPageList(resultList);
      return pageHolder;
    } catch (Exception e) {
      e.printStackTrace();
      LOGGER.error(
          "queryTeacherManagerThesisReviewPageList [{}] error," + this.getClass().getName()
              + "=[{}]",
          this.getClass().getName(), ToStringBuilder.reflectionToString(data + "===" + page,
              ToStringStyle.SHORT_PREFIX_STYLE));
      throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(), e);
    }
  }

  /**
   * @see cn.qidisoft.edu.hzjt.service.IKtglReviewGroupService#queryTeacherThesisReviewList(cn.qidisoft.edu.hzjt.model.KtglTeacherThesisReview)
   */
  @Override
  public List<KtglTeacherThesisReview> queryTeacherThesisReviewList(
      KtglTeacherThesisReview ktglTeacherThesisReview) {
    try {
      return ktglTeacherThesisReviewMapper.queryTeacherThesisReviewList(ktglTeacherThesisReview);
    } catch (Exception e) {
      e.printStackTrace();
      LOGGER.error("queryTeacherThesisReviewList [{}] error," + this.getClass().getName() + "=[{}]",
          this.getClass().getName(), ToStringBuilder.reflectionToString(ktglTeacherThesisReview,
              ToStringStyle.SHORT_PREFIX_STYLE));
      throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(), e);
    }
  }

  /**
   * @see cn.qidisoft.edu.hzjt.service.IKtglReviewGroupService#exportTeacherManagerThesisReviewList(cn.qidisoft.edu.hzjt.vo.TeacherThesis)
   */
  @Override
  public List<TeacherThesis> exportTeacherManagerThesisReviewList(TeacherThesis data) {
    try {
      return ktglTeacherThesisReviewMapper.exportTeacherManagerThesisReviewList(data);
    } catch (Exception e) {
      e.printStackTrace();
      LOGGER.error(
          "exportTeacherManagerThesisReviewList [{}] error," + this.getClass().getName() + "=[{}]",
          this.getClass().getName(),
          ToStringBuilder.reflectionToString(data, ToStringStyle.SHORT_PREFIX_STYLE));
      throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(), e);
    }
  }

@Override
public List<KtglReviewGroup> queryList(KtglReviewGroup record) {
	return ktglReviewGroupMapper.queryList(record);
}

}
